home *** CD-ROM | disk | FTP | other *** search
- /**\
- |**| =====================================================================
- |**|
- |**| graphics shell.c
- |**|
- |**| This file is a shell that can be used to build "new" Graphics
- |**| applications. It contains all of the required calls to use the "new"
- |**| Graphics routines and QuickDraw (i.e. windows) together. You "quit" the
- |**| application by clicking in the close box. This shell does not use a menu.
- |**|
- |**| The application is expected to supply the following functions which
- |**| are called by this shell:
- |**| void DoInitialization (WindowPtr);
- |**| void DoDraw (WindowPtr);
- |**| void DoDispose (WindowPtr);
- |**| void DoClick (gxPoint, WindowPtr);
- |**| void DoIdle (WindowPtr);
- |**|
- |**|
- |**| Change History:
- |**| 3/90 New
- |**| 6/91 PLA Updated the shell to reflect the changes in
- |**| "Graphics" v1.0d21.2.
- |**| 6/92 PLA Made the following variables global: gDebugging,
- |**| gGiveMeValidation,gGraphicsHeapSize.
- |**| See the comments for detail in this file.
- |**| 9/93 PLA Updated files to work with the ß2 "GXified"
- |**| interface files.
- |**| Worked around a problem with GXConvertQDPoint(..)
- |**| see the comments within the GetWindowBoundsShape(..)
- |**| call for details (below).
- |**| 12/93 PLA Updated the comment above the GXNewGraphicsCLient call
- |**| to reflect the new fucntionality of this call in ß3.
- |**| Removed the work around with GXConvertQDPoint (..)
- |**| where the call would not survive validation under ß2.
- |**| Under ß3 the call suceeds when validation is set. PLA
- |**| 3/94 PLA Removed the following varaibles: gDebugging &
- |**| gGiveMeValidation see the comments for details (below).
- |**| Added a Gestalt check to see if GX has been installed.
- |**| Added extensive error checking to make sure the GX graphics
- |**| client has been created and the GX heap correctly allocated.
- |**| This shell will shut down at start-up and alert the user
- |**| to the problem and bail if: GX is not installed, we cannot
- |**| create the graphics client, or create a GX heap.
- |**| 4/94 PLA Added the gxPoint as a parameter to the DoClick (..)
- |**| function to support hit testing.
- |**| Added a check to see if the PowerPC QuickDrawGXLib is
- |**| installed. Search on "powerc" to find all of the changes
- |**| added to support Native QuickDraw GX on a Power Mac. PLA
- |**| 8/94 DH More changes made for universal header and clarity
- |**| Removed GetWindowBoundsShape(..) since it wasn't being
- |**| used after the workaround was removed 19/93
- |**| 4/96 cnn Changed #if condition which surrounds the definition
- |**| of "qd" to work with MPW 3.4. Added (Ptr) coercion to
- |**| kUnresolvedSymbolAddress to allow comparison to
- |**| GXNewGraphicsClient.
- |**|
- |**| ©1990-1996 Apple Computer, Inc.
- |**| All rights reserved.
- |**|
- |**| =====================================================================
- \**/
-
-
-
- #include "graphics shell.h"
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| ENUMS
- |**| ---------------------------------------------------------------------
- \**/
- enum { rWindResource = 128 };
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| GLOBALS
- |**| ---------------------------------------------------------------------
- \**/
- Boolean gQuitting = false;
- long gSleep = 0;
- WindowPtr gWindow;
- gxViewPort gTheWindowsViewPort;
-
- #if GENERATINGCFM
- QDGlobals qd;
- #else
- #ifndef SYMANTEC_C
- #ifndef SYMANTEC_CPLUS
- #define __MPW_ONLY__
- #endif
- #endif
-
- #if defined (__SC__) && defined (__MPW_ONLY__)
- QDGlobals qd;
- #endif
-
- #undef __MPW_ONLY__
- #endif
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| main()
- |**| ---------------------------------------------------------------------
- \**/
- void main()
- {
- CursHandle theCurs;
- long theFeature;
- Boolean debuggingInitInstalled = false;
- gxGraphicsClient client;
-
- InitToolbox();
-
- theCurs = GetCursor(watchCursor);
- SetCursor(*theCurs);
-
- CheckQuickDrawGX();
- if (gQuitting)
- {
- SetCursor(&qd.arrow);
- (void) StopAlert(rNoQuickDrawGXID, NULL);
- return;
- }
-
-
- //
- // The QuickDraw GX gestaltGraphicsAttr Gestalt attribute can be used to determine if:
- // the graphics piece of GX has been loaded, the QuickDrawGX debugging init is installed,
- // or your are running the PowerPC version.
- //
- // In our case, we only need to know if the debugging init was installed. If it is,
- // we will enable the QuickDraw GX validation and notice handling features.We define
- // debuggingInitInstalled as true to enable GX validation and notice handler
- // within the SetUpGXDebuggingWorld function.
- //
- if ( (Gestalt(gestaltGraphicsAttr, &theFeature) == noErr) )
- if ( (theFeature & gestaltGraphicsIsDebugging) == gestaltGraphicsIsDebugging )
- debuggingInitInstalled = true;
-
-
- client = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
-
- //
- // After we attempted to create the graphics client, we need to determine if the call
- // succeeded. If the call did not (as in the case for all GX functions), "client" will
- // be nil. If it is, we alert the user to the problem. Otherwise, we will attempted to
- // allocate the GX heap below...
-
- if ( client )
- {
- // Initialize the new graphics environment and create the GX heap.
-
- GXEnterGraphics();
-
- //
- // Calling GXEnterGraphics allocates the memory within the GX heap. The only reason the
- // call would not succeed is if there is not enough memory. In this case, the graphics
- // error which will be posted is -27999 (out of memory). At this point, we have not
- // installed an error handler, so we check for the error number corresponding to the
- // out of memory error.
-
- if ( GXGetGraphicsError( nil ) != out_of_memory )
- {
- SetUpGXDebuggingWorld (debuggingInitInstalled);
-
- //
- // Create a window and attach a GX viewPort to it. By attaching the viewPort to
- // the window will make sure that when a user moves or resizes the window all of
- // the GX drawing will occur within window.
- //
- // By the way, you cannot directly manipulate the parent viewPort attached to the
- // window, you will recieve a graphics error. This viewPort can only be manipulated
- // by the GX system. If you want to manipulate a viewPort attached to a window, it
- // _must_ be a child viewPort attached to the the parent viewPort attached to the
- // window.
-
- gWindow = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
-
- gTheWindowsViewPort = GXNewWindowViewPort(gWindow);
-
- GXIgnoreGraphicsNotice(transform_already_set);
- SetDefaultViewPort(gTheWindowsViewPort);
- GXPopGraphicsNotice();
-
-
- // Create the GX shapes we are going to draw to the window.
-
- DoInitialization(gWindow);
-
- SetCursor(&qd.arrow);
-
- while (!gQuitting)
- EventLoop(); // loop until the window is closed
-
- // Leaving. Close all the windows so we get rid of any data we or GX created. Then,
- // dispose of the common colors and exit the GX printing and graphics environment.
-
- DoDispose(gWindow);
-
- GXExitGraphics(); // Deallocate all of the default structures
-
- } else DebugStr ("\p Unfortunately, there is not enough memory for GX, please quit an app...");
- } else DebugStr ("\p Unfortunately, there is not enough memory for GX, please quit an app...");
-
- GXDisposeGraphicsClient(client);
- }
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| InitToolbox()
- |**| ---------------------------------------------------------------------
- \**/
- void InitToolbox (void)
- {
- // Generic heap initialization.
-
- MaxApplZone();
- MoreMasters(); MoreMasters(); MoreMasters();
- MoreMasters(); MoreMasters(); MoreMasters();
-
- // Start up the toolbox so we can notify people if there's a problem
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitDialogs(nil);
- InitCursor();
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| CheckQuickDrawGX()
- |**| Before making any calls, we'll see if QuickDraw GX is available.
- |**| If it's not, then set gQuitting=true so we can avoid doing any of
- |**| the rest of this application.
- |**| ---------------------------------------------------------------------
- \**/
- void CheckQuickDrawGX (void)
- {
- long theFeature;
-
- // Before making any calls, we'll see if QuickDraw GX is available. If it's not,
- // we'll avoid doing any of the rest of this application
-
- if (Gestalt(gestaltGraphicsVersion, &theFeature) != noErr)
- gQuitting = true; // QuickDraw GX is not available
-
- #ifdef powerc
- // This is a sanity check to see if the PowerPC QuickDrawGXLib is installed.
- // Since we are "weak" linked to QuickDrawGXLib, the Process Manager will
- // launch us even if QuickDrawGXLib is missing. If it's missing, the
- // Code Fragment Manager will leave the address of the functions we call
- // unresolved, and we would crash if we tried to call that function. So,
- // we do a check here and should be prepared to exit gracefully if the
- // library is missing. This could happen if the user has installed a
- // 68K only version of QuickDraw GX. It could also happen if the user has
- // taken the QuickDraw GX extension out of the Extensions Folder.
- //
- // Note - We could check this against any function we call in the library.
- // GXNewGraphicsClient is the first function we call, so it's convenient.
-
- if ( (Ptr)GXNewGraphicsClient == (Ptr)kUnresolvedSymbolAddress )
- gQuitting = true; // QuickDraw GX is not available in Power Mac
- #endif
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| SetUpGXDebuggingWorld()
- |**| This function enables the GX error handling capabilities and validation
- |**| routines. The validation routines are only enabled, if the user has
- |**| installed the QuickDraw GX debugging init. These routines are not
- |**| available with the non-debugging init. Calling them when they are not
- |**| installed will not cause any problems, but it will cause unnecessary
- |**| work to be done by your application and the GX dispatcher.
- |**| ---------------------------------------------------------------------
- \**/
- void SetUpGXDebuggingWorld (Boolean debuggingInitInstalled)
- {
- //
- // We set-up GX validation, if the user has installed the "GXGraphics (debug)" init (formerly
- // named "aSecretGraphics.debug). This validation setting is the reccommended setting while
- // you are developing your GX application. As you increase the amount of validation, the drawing
- // speed will SLOW down due to all of the internal checking.
- //
- // For additional details regarding the various levels of validation, please see the
- // QuickDraw GX : Environment & Utilities book.
- //
- if ( debuggingInitInstalled ) GXSetValidation(gxPublicValidation + gxTypeValidation);
-
- //
- // Calling SetGraphicsLibraryErrors will install a GX error and warning handler. This
- // call is provided by the QuickDraw GX "graphics debugging library". Any time a GX error
- // or warning is generated, it will be posted to Macsbug.
- //
- SetGraphicsLibraryErrors ();
-
- //
- // If the user has installed the GX debugging version, we install a notice handler. Why? The
- // GX notice handling capabilities are only available with the debugging version.
- //
- if ( debuggingInitInstalled ) SetGraphicsLibraryNotices();
- }
-
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| EventLoop()
- |**| ---------------------------------------------------------------------
- \**/
- void EventLoop ()
- {
- EventRecord event;
-
- if ( WaitNextEvent(everyEvent, &event, gSleep, nil) )
- MyDoEvent(&event);
- else
- DoIdle(FrontWindow());
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| MyDoEvent()
- |**| ---------------------------------------------------------------------
- \**/
- void MyDoEvent (EventRecord *event)
- {
- WindowPtr window; // temporarily used to hold
- GrafPtr oldPort;
- unsigned long mssg;
-
- mssg = event->message;
- switch(event->what)
- {
- case updateEvt:
- BeginUpdate((WindowPtr) mssg);
- SetPort(gWindow);
- DoDraw(gWindow);
- EndUpdate((WindowPtr) mssg);
- break;
-
- case mouseDown:
- DoMouseDown(event);
- break;
-
- case osEvt:
- if ( (mssg>>24) // if high byte of message indicates
- == suspendResumeMessage ) // this is suspend/resume event
- {
- if (mssg & resumeFlag) // if resume event
- gSleep = 0; // we're switching back from another app so speed up
- else // if suspend event
- gSleep = 80; // we're switching to another app so slow down...
- }
- break;
-
- }
- }
-
-
- /**\
- |**| ---------------------------------------------------------------------
- |**| DoMouseDown
- |**| handle DoMouseDown events
- |**| ---------------------------------------------------------------------
- \**/
- void DoMouseDown (EventRecord *event)
- {
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- clickArea = FindWindow( event->where, &window );
- switch (clickArea)
- {
- case inSysWindow:
- SystemClick(event, window);
- break;
-
- case inDrag:
- screenRect = (**GetGrayRgn()).rgnBBox;
- DragWindow( window, event->where, &screenRect );
- break;
-
- case inContent:
- if ( window == FrontWindow())
- {
- gxPoint mouseLoc;
- Point currMouseLoc;
-
- currMouseLoc.h = event->where.h;
- currMouseLoc.v = event->where.v;
-
- GXConvertQDPoint(&currMouseLoc, gTheWindowsViewPort, &mouseLoc);
-
- DoClick(mouseLoc, gWindow);
- }
- else
- SelectWindow(window);
- break;
-
- case inGoAway:
- if ( TrackGoAway(window, event->where) )
- gQuitting = true;
- break;
-
- }
- }
-
-